<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}
</style>

<template>
<div class="department">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加部门</a>
    </div>
    <div class="layui-row">
        <div class="layui-col-lg4 select-item" style="text-align: right">
            <div class="layui-input-inline" style="width: 300px">
                <input type="text" v-model="searchName" placeholder="部门名称" class="layui-input key">
            </div>
            <button type="button" class="layui-btn sou" @click="search">搜索</button>
        </div>
    </div>
    <table class="layui-table layui-form">

        <thead>
            <tr>
                <th>名称</th>
                <th>编码</th>
                <th>所属公司</th>
                <th>部门类型</th>
                <th>负责人</th>
                <th>备注</th>
                <th>创建人</th>
                <th>创建时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in departments">
                <td>{{val.name}}</td>
                <td>{{val.code}}</td>
                <td>{{val.orgname}} </td>
                <td>{{val.typename}}</td>
                <td>{{val.manager}}</td>
                <td>{{val.remark}}</td>
                <td>{{val.operatorname}}</td>
                <td>{{val.createtime}}</td>
                <td><a href="javascript:;" @click="edit(val.guid)">修改</a> | <a href="javascript:;" @click="del(val.guid,val.code)">删除</a></td>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
export default {
    name: 'Department',
    components: {
        Pagination
    },
    data() {
        return {
            departments: [],
            currentPage: 1,
            totalPage: 0,
            searchName: ''
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
        });

    },
    methods: {
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = {};
            if (type != 'search') {
                postData.page = that.currentPage;
            } else {
                postData.page = 1;
            }
            postData.name = that.searchName;

            $.ajax({
                url: '/sales/Department!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    if (res.info != undefined && res.info.rows != undefined) {
                        //分页数据赋值
                        // that.currentPage = res.info.indexPage;
                        that.totalPage = res.info.pageCount;
                        that.departments = res.info.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddDepartment');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditDepartment');
        },
        del: function (id, likeCode) {
            let that = this;
            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                if (!that.checkDel(id, likeCode)) {
                    alert("此部门下有权限分配，或销售单，不能删除！");
                    layer.close(loader);
                    return;
                }

                $.ajax({
                    url: '/sales/Department!del.action',
                    type: "post",
                    data: {
                        guid: id
                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.info == "ok") {
                            that.toast("删除成功");
                            that.getPageData();
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
            })
        },
        checkSubDepart: function (guid, likeCode) {
            var result = false;
            $.ajax({
                url: '/sales/Department!checkSubCode.action',
                async: false,
                data: {
                    likeCode: likeCode
                },
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res.info == "ok") {
                        result = true;
                    }
                }
            });
            return result;

        },
        checkDel: function (guid, likeCode) {
            var result = false;
            $.ajax({
                url: '/sales/Department!checkDel.action',
                async: false,
                data: {
                    guid: guid
                },
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res.info == "ok") {
                        result = true;
                    }
                }
            });
            return result;

        }
    }
}
</script>
